package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.app.App;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.ContractException;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeoutException;

@Service
public class CarRecordService {
    public Contract contract;
    Gateway gateway;
    Network network;

    public CarRecordService() {
        connect();
    }

    public void connect() {
        try (Gateway gateway = App.connect()) {
            this.gateway = gateway;
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.network = gateway.getNetwork("mychannel");
        this.contract = network.getContract("basic");
    }

    public boolean checkContract() {
        return contract != null;
    }

    public String getAll() throws ContractException {
        return new String(contract.evaluateTransaction("GetAllAssets"));
    }

    public String getOne(String assetID) throws ContractException {
        if (!exists(assetID)) {
            return null;
        }
        return new String(contract.evaluateTransaction("ReadAsset", assetID));
    }

    public boolean exists(String assetID) throws ContractException {
        return Boolean.parseBoolean(new String(contract.evaluateTransaction("AssetExists", assetID)));
    }

    public boolean create(JSONObject msg) {
        try {
            contract.submitTransaction("CreateAsset",
                    (String) msg.get("assetID"),
                    (String) msg.get("rID"),
                    (String) msg.get("cID"),
                    (String) msg.get("aProvince"),
                    (String) msg.get("amount"),
                    (String) msg.get("aProof"),
                    (String) msg.get("pStart"),
                    (String) msg.get("pEnd"),
                    (String) msg.get("status"),
                    (String) msg.get("aStart"),
                    (String) msg.get("aEnd"),
                    (String) msg.get("dpNum"));
        } catch (ContractException | TimeoutException | InterruptedException e) {
//            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean update(JSONObject msg) {
        try {
            contract.submitTransaction("UpdateAsset",
                    (String) msg.get("assetID"),
                    (String) msg.get("rID"),
                    (String) msg.get("cID"),
                    (String) msg.get("aProvince"),
                    (String) msg.get("amount"),
                    (String) msg.get("aProof"),
                    (String) msg.get("pStart"),
                    (String) msg.get("pEnd"),
                    (String) msg.get("status"),
                    (String) msg.get("aStart"),
                    (String) msg.get("aEnd"),
                    (String) msg.get("dpNum"));
        } catch (ContractException | TimeoutException | InterruptedException e) {
//            e.printStackTrace();
            return false;
        }
        return true;
    }
}
